<?php
/**
 * @Created on 2021/05/25
 * @Author Qi
*/
namespace app\admin\controller;
use think\Controller;
use think\Db;

class Agent extends Base{

    //代理商管理
    public function index(){

        $where = array();
        $name=request()->param('name');
        if($name){
            $where['name'] = array('like',"%{$name}%");
        }
        $company=request()->param('company');
        if($company){
            $where['company'] = array('like',"%{$company}%");
        }
        $tel=request()->param('tel');
        if($tel){
            $where['tel'] = array('like',"%{$tel}%");
        }
        $email=request()->param('email');
        if($email){
            $where['email'] = array('like',"%{$email}%");
        }
        $start=request()->param('start');
        if($start){
            $where['add_time'] = array('egt',strtotime($start));
        }
        $end=request()->param('end');
        if($end){
            $where['add_time'] = array('elt',strtotime($end));
        }
        if(!empty($start) && !empty($end)){
            $where['add_time'] = array('between time',["{$start}","{$end}"]);
        }
        $list=db('agent')->where($where)->order('id desc')->paginate(50,false,['query'=>request()->param()]);
        $this->assign('list',$list);
        $this->assign('name',$name);
        $this->assign('company',$company);
        $this->assign('tel',$tel);
        $this->assign('email',$email);
        $this->assign('start',$start);
        $this->assign('end',$end);
        return $this->fetch();
    }

    //添加
    public function add(){

        if(request()->isPost()){
            $name = trim(request()->param('name'));
            $company = trim(request()->param('company'));
            $tel = trim(request()->param('tel'));
            $email = trim(request()->param('email'));
            $status = request()->param('status');
            $data['name'] = $name;
            $data['company'] = $company;
            $data['tel'] = $tel;
            $data['email'] = $email;
            $data['status'] = $status;
            $data['add_time'] = time();
            $data['do_user'] = session('username');
            $data['do_id'] = session('uid');
            $true=db('agent')->insert($data);
            if($true){
                system_log('agent','添加代理商');
                return json(['status'=>1,'message'=>'添加成功']);
            }else{
                return json(['status'=>0,'message'=>'添加失败']);
            }
        }else{
            return $this->fetch();
        }
    }

    //修改
    public function edit(){
        if(request()->isPost()){
            $id = request()->param('id');
            $name = trim(request()->param('name'));
            $company = trim(request()->param('company'));
            $tel = trim(request()->param('tel'));
            $email = trim(request()->param('email'));
            $status = request()->param('status');
            $data['name'] = $name;
            $data['company'] = $company;
            $data['tel'] = $tel;
            $data['email'] = $email;
            $data['status'] = $status;
            $data['do_user'] = session('username');
            $data['do_id'] = session('uid');
            $true=db('agent')->where('id',$id)->update($data);
            if($true){
                system_log('agent','修改代理商');
                return json(['status'=>1,'message'=>'修改成功']);
            }else{
                return json(['status'=>0,'message'=>'修改失败']);
            }
        }else{
            $id=request()->param('id');
            $info=db('agent')->where('id',$id)->find();
            $this->assign('info',$info);
            return $this->fetch();
        }
    }

    //删除
    public function del(){
        $id=request()->param('id');
        $true=db('agent')->where('id',$id)->delete();
        if($true){
            system_log('agent','删除代理商');
            return json(['status'=>1,'message'=>'删除成功']);
        }else{
            return json(['status'=>0,'message'=>'删除失败']);
        }
    }

}